<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/pages/ci/base/ci_type_preview_wp.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/ci_types/tcms_ci_article_dbc.class.php');


class CI_Article_Preview_WP extends CI_Type_Preview_WP {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_ci/');

		return $ret;
	}


	protected function ci_event($ci_article_id) {
		$dbc = $this->get_dbc();

		$ci_article = new TCMS_CI_Article_DBC();
		if ($ci_article->load_by_id($dbc, $ci_article_id)) {
			$ci_article_prep = $this->prepare_article($ci_article);
			$this->export('ci_article', $ci_article_prep);

			$ci_id = $ci_article->get_ci()->get_id();
			$this->export('ci_id', $ci_id);

			$authors_attr_type = new TCMS_CI_Type_Attr_DBC();
			if (!$authors_attr_type->load_by_type_and_hid($dbc, $ci_article->get_ci()->get_type(), 'authors')) {
				throw new Tangra_Module_Exception('Cannot find "authors" CI_Type_Attr');
			}
			$this->export('authors_attr_type_id', $authors_attr_type->get_id());

			$authors_nomenclature_hid = $authors_attr_type->get_params();
			$authors_nomenclature = new TCMS_Nomenclature_DBC();
			if (!$authors_nomenclature->load_by_hid($dbc, $authors_nomenclature_hid)) {
				throw new Tangra_Module_Exception('Cannot find nomenclature with hid = '.$authors_nomenclature_hid);
			}


			$this->export_attachments($dbc, $ci_id);
			$this->export_images($dbc, $ci_id);

			$view = $this->get_view('default');
		} else {
			$view = $this->get_view('not_exist');
		}

		return $view;
	}


	private function prepare_article(TCMS_CI_Article_DBC $ci_article) {
		$ret['id'] = $ci_article->get_ci()->get_id();
		$ret['title_clean'] = strip_tags($ci_article->get_title()->get_value());
		$ret['title'] = $ci_article->get_title()->get_value();
		$ret['content'] = $ci_article->get_content()->get_value();
		$ret['intro'] = $ci_article->get_intro()->get_value();
		$ret['created'] = $ci_article->get_ci()->get_created();

		return $ret;
	}
}
